lock="/tmp/$SCRIPT_NAME.pid"
mkdir -p $(dirname $lock)

if [[ -f "$lock" ]]; then
pid=$(cat $lock)
while [ -e /proc/$pid ]; do
  echo "$SCRIPT_NAME: pid $pid running , trying kill it"
  kill $pid
  sleep 1
done
fi

trap "rm -f $lock; exit $?" INT TERM EXIT
echo $PID > $lock
